package net.zrnecko.km.admin.client;

import net.zrnecko.km.client.PersistenceService;
import net.zrnecko.km.client.PersistenceServiceAsync;
import net.zrnecko.km.dto.PlayerDTO;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;

public class Admin implements EntryPoint {

	public void onModuleLoad() {

		final Label testLabel = new Label();
		testLabel.setText("Admin page");
		RootPanel.get().add(testLabel);

		final Label debugLabel = new Label();
		debugLabel.setText("Submitting file...");
		debugLabel.setVisible(false);

		// Enclosing form
		final FormPanel form = new FormPanel();
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
		form.setAction("/kalicmasters/upload");

		// Add an event handler to the form.
		form.addSubmitHandler(new SubmitHandler() {

			public void onSubmit(SubmitEvent event) {
				// TODO Auto-generated method stub
				debugLabel.setVisible(true);
			}

		});

		// File upload element
		final FileUpload fileUpload = new FileUpload();
		fileUpload.setName("fileUpload");
		// fileUpload.ensureDebugId("cwFileUpload");

		// List box determining uploaded file type
		final ListBox fileTypeDropDown = new ListBox(false);
		fileTypeDropDown.setName("fileTypeDropDown");
		String[] fileTypes = { "Tournaments", "Results", "Players" };
		for (int i = 0; i < fileTypes.length; i++) {
			fileTypeDropDown.addItem(fileTypes[i]);
		}

		//
		final Label infoMessage = new Label("Submitting...");
		infoMessage.setVisible(false);

		// Submit button
		final Button submitButton = new Button("Submit", new ClickHandler() {
			public void onClick(ClickEvent event) {
				form.submit();
				infoMessage.setVisible(true);
			}
		});

		final VerticalPanel panel = new VerticalPanel();
		
		
		

		// Composing form
		form.setWidget(panel);
		
		
		
		panel.add(fileUpload);
		panel.add(fileTypeDropDown);
		panel.add(submitButton);
		panel.add(infoMessage);
		panel.add(debugLabel);
		
	    final PersistenceServiceAsync ps = GWT
			.create(PersistenceService.class);
		final VerticalPanel panel2 = new VerticalPanel();
		final TextBox playerName = new TextBox();
		final Label status = new Label();
		panel2.add(playerName);
		
		final Button addPlayerButton = new Button("Add player", new ClickHandler() {
			public void onClick(ClickEvent event) {
				final String name = playerName.getText();
				// TODO name validation - not empty?
				PlayerDTO p = new PlayerDTO();
				p.setName(name);
				ps.persistPlayer(p, new AsyncCallback<Void>() {
					
					public void onSuccess(Void result) {
						status.setText("Player saved");
						
					}
					
					public void onFailure(Throwable caught) {
						status.setText("Saving player failed");
						
					}
				});
				
				
				
				
				
				
			}
		});
		panel2.add(addPlayerButton);
		panel2.add(status);
		
		RootPanel.get().add(form);
		RootPanel.get().add(panel2);

	}
}
